<?php

namespace App\HttpController;

use App\Service\RentService;
use App\Service\SensitiveService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="房屋出租")
 */
class Rent extends BaseController
{
    /**
     * @Api(name="获取房屋出租列表", path="/rent/rentList", version="1.0.0")
     * @ApiDescription(value="获取房屋出租列表")
     * @Method(allow={POST})
     * @Param(name="type", defaultValue=0, description="类型", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function rentList($type, $limit, $page): bool
    {
        $res = RentService::getInstance()->rentList($this->uid, $type, $limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="发布房屋出租", path="/rent/release", version="1.0.0")
     * @ApiDescription(value="发布房屋出租")
     * @Method(allow={POST})
     * @Param(name="title", required="", description="房屋名称", from={JSON})
     * @Param(name="rent_type", required="", description="出租类型", from={JSON})
     * @Param(name="amount_type", required="", description="付款类型", from={JSON})
     * @Param(name="house_type", required="", description="户型", from={JSON})
     * @Param(name="floor_type", required="", description="楼层", from={JSON})
     * @Param(name="arrow_type", required="", description="方向类型", from={JSON})
     * @Param(name="renovation_type", required="", description="装修类型", from={JSON})
     * @Param(name="area", required="", description="面积", from={JSON})
     * @Param(name="equipment_type", required="", description="设备", from={JSON})
     * @Param(name="span", required="", description="标签", from={JSON})
     * @Param(name="content", required="", description="房屋介绍", from={JSON})
     * @Param(name="location", required="", description="坐标", from={JSON})
     * @Param(name="address", required="", description="所在位置", from={JSON})
     * @Param(name="price", required="", description="租金", from={JSON})
     * @Param(name="images", required="", description="图片", from={JSON})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function release($title, $rent_type, $amount_type, $house_type, $floor_type, $arrow_type, $renovation_type, $area, $equipment_type, $span, $content, $location, $address, $price, $images): bool
    {
        $res = RentService::getInstance()->release($this->uid, $title, $rent_type, $amount_type, $house_type, $floor_type, $arrow_type, $renovation_type, $area, $equipment_type, $span, $content, $location, $address, $price, $images);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取房屋租赁详情", path="/rent/getRentDetail", version="1.0.0")
     * @ApiDescription(value="获取房屋租赁详情")
     * @Method(allow={POST})
     * @Param(name="id", required="", description="id", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getRentDetail($id): bool
    {
        $res = RentService::getInstance()->getRentDetail($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的发布", path="/rent/getMyRent", version="1.0.0")
     * @ApiDescription(value="获取我的发布")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMyRent(): bool
    {
        $res = RentService::getInstance()->getMyRent($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="房屋租赁点赞", path="/rent/likeRent", version="1.0.0")
     * @ApiDescription(value="房屋租赁点赞")
     * @Method(allow={POST})
     * @Param(name="id", required="", description="id", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function likeRent($id): bool
    {
        $res = RentService::getInstance()->likeRent($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="房屋租赁评论")
     * @Api(name="房屋租赁评论", path="/rent/commentRent", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="id", description="评论id", defaultValue=0, from={JSON})
     * @Param(name="gid", description="动态id", required="", notEmpty="", from={JSON})
     * @Param(name="content", description="内容", required="", notEmpty="", from={JSON})
     * @Param(name="images", description="图片", required="", notEmpty="", from={JSON})
     * @Param(name="star", description="星级", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function commentRent($id, $gid, $content, $images, $star): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $res = RentService::getInstance()->commentRent($this->uid,$id, $gid, $content, $images, $star);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="查看房屋租赁评论")
     * @Api(name="查看房屋租赁评论", path="/rent/getRentComment", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="gid", description="动态id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     * @throws Exception
     */
    public function getRentComment($gid): bool
    {
        $res = RentService::getInstance()->getRentComment($this->uid, $gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}